package cn.cvs.servlet;


import cn.cvs.dao.PowerBatteryDao;
import cn.cvs.pojo.Powerbattery;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/Update")
public class Update extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Powerbattery powerbattery=new Powerbattery();
        powerbattery.setUsername(req.getParameter("userName"));
        powerbattery.setTime(req.getParameter("time"));
        powerbattery.setCityname(req.getParameter("cityName"));
        powerbattery.setCarname(req.getParameter("carName"));
        powerbattery.setPressurevalue(Integer.parseInt(req.getParameter("pressureValue")));
        powerbattery.setId(Integer.parseInt(req.getParameter("id")));
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int update = sqlSession.getMapper(PowerBatteryDao.class).updateById(powerbattery);
        sqlSession.commit();
        if (update>0){
            out.print("<script>\n" +
                    "    window.location=\"index.jsp\";\n" +
                    "    alert(\"修改成功\")\n" +
                    "</script>");
        }else {
            out.print("<script>\n" +
                    "    window.location=\"Update.jsp\";\n" +
                    "    alert(\"修改失败\")\n" +
                    "</script>");
        }
    }
}
